home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / Interfaces&Libraries / Universal / Interfaces / AIncludes / PEFBinaryFormat.a < prev    next >
Encoding:
Text File  |  1998-02-12  |  37.5 KB  |  732 lines  |  [TEXT/MPS ]

  1. ;
  2. ;    File:        PEFBinaryFormat.a
  3. ;
  4. ;    Contains:    PEF Types and Macros
  5. ;
  6. ;    Version:    Technology:    Master Interfaces
  7. ;                Release:    Universal Interfaces 3.1
  8. ;
  9. ;    Copyright:    © 1993-1998, 1998 by Apple Computer, Inc., all rights reserved.
  10. ;
  11. ;    Bugs?:        Please include the the file and version information (from above) with
  12. ;                the problem description.  Developers belonging to one of the Apple
  13. ;                developer programs can submit bug reports to:
  14. ;
  15. ;                    devsupport@apple.com
  16. ;
  17. ;
  18.  
  19.  
  20.     IF &TYPE('__PEFBINARYFORMAT__') = 'UNDEFINED' THEN
  21. __PEFBINARYFORMAT__ SET 1
  22.  
  23.     IF &TYPE('__MACTYPES__') = 'UNDEFINED' THEN
  24.     include 'MacTypes.a'
  25.     ENDIF
  26.  
  27.  
  28. ;  -----------------------------------------------------------------------------------------    
  29. ;  Almost all types are padded for natural alignment.  However the PEFExportedSymbol type is    
  30. ;  10 bytes long, containing two 32 bit fields and one 16 bit field.  Arrays of it must be        
  31. ;  packed, so it requires "68K" alignment.  Setting this globally to 68K should also help        
  32. ;  ensure consistent treatment across compilers.                                                
  33.  
  34. ;  ======================================================================================== 
  35. ;  Overall Structure 
  36. ;  ================= 
  37.  
  38. ;  -------------------------------------------------------------------------------------------    
  39. ;  This header contains a complete set of types and macros for dealing with the PEF executable    
  40. ;  format.  While some description is provided, this header is not meant as a primary source    
  41. ;  of documentation on PEF.  An excellent specification of PEF can be found in the Macintosh    
  42. ;  Runtime Architectures book.  This header is primarily a physical format description.  Thus    
  43. ;  it depends on as few other headers as possible and structure fields have obvious sizes.        
  44. ;                                                                                                  
  45. ;  The physical storage for a PEF executable is known as a "container".  This refers to just    
  46. ;  the executable itself, not the file etc.  E.g. if five DLLs are packaged in a single file's    
  47. ;  data fork, that one data fork has five containers within it.                                    
  48. ;                                                                                                  
  49. ;  A PEF container consists of an overall header, followed by one or more section headers,        
  50. ;  followed by the section name table, followed by the contents for the sections.  Some kinds    
  51. ;  of sections have specific internal representation.  The "loader" section is the most common    
  52. ;  of these special sections.  It contains information on the exports, imports, and runtime        
  53. ;  relocations required to prepare the executable.  PEF containers are self contained, all        
  54. ;  portions are located via relative offsets.                                                    
  55. ;                                                                                                  
  56. ;                                                                                                  
  57. ;             +-------------------------------+                                                    
  58. ;             |        Container Header        |    40 bytes                                        
  59. ;             +-------------------------------+                                                    
  60. ;             |        Section 0 header        |    28 bytes each                                    
  61. ;             |...............................|                                                    
  62. ;             |            - - - -                |                                                    
  63. ;             |...............................|                                                    
  64. ;             |        Section n-1 header        |                                                    
  65. ;             +-------------------------------+                                                    
  66. ;             |        Section Name Table        |                                                    
  67. ;             +-------------------------------+                                                    
  68. ;             |        Section x raw data        |                                                    
  69. ;             +-------------------------------+                                                    
  70. ;              |            - - - -                |                                                    
  71. ;             +-------------------------------+                                                    
  72. ;             |        Section y raw data        |                                                    
  73. ;             +-------------------------------+                                                    
  74. ;                                                                                                  
  75. ;                                                                                                  
  76. ;  The sections are implicitly numbered from 0 to n according to the order of their headers.    
  77. ;  The headers of the instantiated sections must precede those of the non-instantiated            
  78. ;  sections.  The ordering of the raw data is independent of the section header ordering.        
  79. ;  Each section header contains the offset for that section's raw data.                            
  80.  
  81. ;  =========================================================================================== 
  82. ;  Container Header 
  83. ;  ================ 
  84.  
  85. PEFContainerHeader        RECORD 0
  86. tag1                     ds.l    1                ; offset: $0 (0)        ;  Must contain 'Joy!'. 
  87. tag2                     ds.l    1                ; offset: $4 (4)        ;  Must contain 'peff'.  (Yes, with two 'f's.) 
  88. architecture             ds.l    1                ; offset: $8 (8)        ;  The ISA for code sections.  Constants in CodeFragments.h. 
  89. formatVersion             ds.l    1                ; offset: $C (12)        ;  The physical format version. 
  90. dateTimeStamp             ds.l    1                ; offset: $10 (16)        ;  Macintosh format creation/modification stamp. 
  91. oldDefVersion             ds.l    1                ; offset: $14 (20)        ;  Old definition version number for the code fragment. 
  92. oldImpVersion             ds.l    1                ; offset: $18 (24)        ;  Old implementation version number for the code fragment. 
  93. currentVersion             ds.l    1                ; offset: $1C (28)        ;  Current version number for the code fragment. 
  94. sectionCount             ds.w    1                ; offset: $20 (32)        ;  Total number of section headers that follow. 
  95. instSectionCount         ds.w    1                ; offset: $22 (34)        ;  Number of instantiated sections. 
  96. reservedA                 ds.l    1                ; offset: $24 (36)        ;  Reserved, must be written as zero. 
  97. sizeof                     EQU *                    ; size:   $28 (40)
  98.                         ENDR
  99.  
  100. kPEFTag1                        EQU        'Joy!'                ; For non-Apple compilers: 0x4A6F7921. 
  101. kPEFTag2                        EQU        'peff'                ; For non-Apple compilers: 0x70656666. 
  102. kPEFVersion                        EQU        $00000001
  103.  
  104.  
  105. kPEFFirstSectionHeaderOffset    EQU        40
  106.  
  107. ;  =========================================================================================== 
  108. ;  Section Headers 
  109. ;  =============== 
  110.  
  111. PEFSectionHeader        RECORD 0
  112. nameOffset                 ds.l    1                ; offset: $0 (0)        ;  Offset of name within the section name table, -1 => none. 
  113. defaultAddress             ds.l    1                ; offset: $4 (4)        ;  Default address, affects relocations. 
  114. totalLength                 ds.l    1                ; offset: $8 (8)        ;  Fully expanded size in bytes of the section contents. 
  115. unpackedLength             ds.l    1                ; offset: $C (12)        ;  Size in bytes of the "initialized" part of the contents. 
  116. containerLength             ds.l    1                ; offset: $10 (16)        ;  Size in bytes of the raw data in the container. 
  117. containerOffset             ds.l    1                ; offset: $14 (20)        ;  Offset of section's raw data. 
  118. sectionKind                 ds.b    1                ; offset: $18 (24)        ;  Kind of section contents/usage. 
  119. shareKind                 ds.b    1                ; offset: $19 (25)        ;  Sharing level, if a writeable section. 
  120. alignment                 ds.b    1                ; offset: $1A (26)        ;  Preferred alignment, expressed as log 2. 
  121. reservedA                 ds.b    1                ; offset: $1B (27)        ;  Reserved, must be zero. 
  122. sizeof                     EQU *                    ; size:   $1C (28)
  123.                         ENDR
  124.  
  125.                                                             ; Values for the sectionKind field. 
  126.                                                             ;    Section kind values for instantiated sections. 
  127. kPEFCodeSection                    EQU        0                    ; Code, presumed pure & position independent. 
  128. kPEFUnpackedDataSection            EQU        1                    ; Unpacked writeable data. 
  129. kPEFPackedDataSection            EQU        2                    ; Packed writeable data. 
  130. kPEFConstantSection                EQU        3                    ; Read-only data. 
  131. kPEFExecDataSection                EQU        6                    ; Intermixed code and writeable data. 
  132.                                                             ; Section kind values for non-instantiated sections. 
  133. kPEFLoaderSection                EQU        4                    ; Loader tables. 
  134. kPEFDebugSection                EQU        5                    ; Reserved for future use. 
  135. kPEFExceptionSection            EQU        7                    ; Reserved for future use. 
  136. kPEFTracebackSection            EQU        8                    ; Reserved for future use. 
  137.  
  138.  
  139.                                                             ; Values for the shareKind field. 
  140. kPEFProcessShare                EQU        1                    ; Shared within a single process. 
  141. kPEFGlobalShare                    EQU        4                    ; Shared across the entire system. 
  142. kPEFProtectedShare                EQU        5                    ; Readable across the entire system, writeable only to privileged code. 
  143.  
  144. ;  =========================================================================================== 
  145. ;  Packed Data Contents 
  146. ;  ==================== 
  147.  
  148. ;  -------------------------------------------------------------------------------------------    
  149. ;  The raw contents of a packed data section are a sequence of byte codes.  The basic format    
  150. ;  has a 3 bit opcode followed by a 5 bit count.  Additional bytes might be used to contain        
  151. ;  counts larger than 31, and to contain a second or third count.  Further additional bytes        
  152. ;  contain actual data values to transfer.                                                        
  153. ;                                                                                                  
  154. ;  All counts are represented in a variable length manner.  A zero in the initial 5 bit count    
  155. ;  indicates the actual value follows.  In this case, and for the second and third counts, the    
  156. ;  count is represented as a variable length sequence of bytes.  The bytes are stored in big    
  157. ;  endian manner, most significant part first.  The high order bit is set in all but the last    
  158. ;  byte.  The value is accumulated by shifting the current value up 7 bits and adding in the    
  159. ;  low order 7 bits of the next byte.                                                            
  160.  
  161.  
  162.                                                             ; The packed data opcodes. 
  163. kPEFPkDataZero                    EQU        0                    ; Zero fill "count" bytes. 
  164. kPEFPkDataBlock                    EQU        1                    ; Block copy "count" bytes. 
  165. kPEFPkDataRepeat                EQU        2                    ; Repeat "count" bytes "count2"+1 times. 
  166. kPEFPkDataRepeatBlock            EQU        3                    ; Interleaved repeated and unique data. 
  167. kPEFPkDataRepeatZero            EQU        4                    ; Interleaved zero and unique data. 
  168.  
  169.  
  170. kPEFPkDataOpcodeShift            EQU        5
  171. kPEFPkDataCount5Mask            EQU        $1F
  172. kPEFPkDataMaxCount5                EQU        31
  173. kPEFPkDataVCountShift            EQU        7
  174. kPEFPkDataVCountMask            EQU        $7F
  175. kPEFPkDataVCountEndMask            EQU        $80
  176.  
  177.  
  178. ;  ------------------------------------------------------------------------------------------    
  179. ;  The following code snippet can be used to input a variable length count.                        
  180. ;                                                                                                  
  181. ;         count = 0;                                                                                
  182. ;         do {                                                                                    
  183. ;             byte = *bytePtr++;                                                                    
  184. ;             count = (count << kPEFPkDataVCountShift) | (byte & kPEFPkDataVCountMask);            
  185. ;         } while ( (byte & kPEFPkDataVCountEndMask) != 0 );                                        
  186. ;                                                                                                  
  187. ;  The following code snippet can be used to output a variable length count to a byte array.    
  188. ;  This is more complex than the input code because the chunks are output in big endian order.    
  189. ;  Think about handling values like 0 or 0x030000.                                                
  190. ;                                                                                                  
  191. ;         count = 1;.                                                                                
  192. ;         tempValue = value >> kPEFPkDataCountShift;                                                
  193. ;         while ( tempValue != 0 ) {                                                                
  194. ;             count += 1;                                                                            
  195. ;             tempValue = tempValue >> kPEFPkDataCountShift;                                        
  196. ;         }                                                                                        
  197. ;                                                                                                  
  198. ;         bytePtr += count;                                                                        
  199. ;         tempPtr = bytePtr - 1;                                                                    
  200. ;         *tempPtr-- = value;        // ! No need to mask, only the low order byte is stored.        
  201. ;         for ( count -= 1; count != 0; count -= 1 ) {                                            
  202. ;             value = value >> kPEFPkDataCountShift;                                                
  203. ;             *tempPtr-- = value | kPEFPkDataCountEndMask;                                        
  204. ;         }                                                                                        
  205.  
  206. ;  =========================================================================================== 
  207. ;  Loader Section 
  208. ;  ============== 
  209.  
  210. ;  ------------------------------------------------------------------------------------------    
  211. ;  The loader section contains information needed to prepare the code fragment for execution.    
  212. ;  This includes this fragment's exports, the import libraries and the imported symbols from    
  213. ;  each library, and the relocations for the writeable sections.                                
  214. ;                                                                                                  
  215. ;             +-----------------------------------+                <-- containerOffset --------+    
  216. ;             |        Loader Info Header            |    56 bytes                                |    
  217. ;             |-----------------------------------|                                            |    
  218. ;             |        Imported Library 0            |    24 bytes each                            |    
  219. ;             |...................................|                                            |    
  220. ;             |            - - -                    |                                            |    
  221. ;             |...................................|                                            |    
  222. ;             |        Imported Library l-1        |                                            |    
  223. ;             |-----------------------------------|                                            |    
  224. ;             |        Imported Symbol 0            |    4 bytes each                            |    
  225. ;             |...................................|                                            |    
  226. ;             |            - - -                    |                                            |    
  227. ;             |...................................|                                            |    
  228. ;             |         Imported Symbol i-1            |                                            |    
  229. ;             |-----------------------------------|                                            |    
  230. ;             |        Relocation Header 0            |    12 bytes each                            |    
  231. ;             |...................................|                                            |    
  232. ;             |            - - -                    |                                            |    
  233. ;             |...................................|                                            |    
  234. ;             |        Relocation Header r-1        |                                            |    
  235. ;             |-----------------------------------|                <-- + relocInstrOffset -----|    
  236. ;             |        Relocation Instructions        |                                            |    
  237. ;             |-----------------------------------|                <-- + loaderStringsOffset --|    
  238. ;             |        Loader String Table            |                                            |    
  239. ;             |-----------------------------------|                <-- + exportHashOffset -----+    
  240. ;             |        Export Hash Slot 0            |    4 bytes each                                
  241. ;             |...................................|                                                
  242. ;             |            - - -                    |                                                
  243. ;             |...................................|                                                
  244. ;             |         Export Hash Slot h-1        |                                                
  245. ;             |-----------------------------------|                                                
  246. ;             |        Export Symbol Key 0            |    4 bytes each                                
  247. ;             |...................................|                                                
  248. ;             |            - - -                    |                                                
  249. ;             |...................................|                                                
  250. ;             |        Export Symbol Key e-1        |                                                
  251. ;             |-----------------------------------|                                                
  252. ;             |        Export Symbol 0                |    10 bytes each                                
  253. ;             |...................................|                                                
  254. ;             |            - - -                    |                                                
  255. ;             |...................................|                                                
  256. ;             |        Export Symbol e-1            |                                                
  257. ;             +-----------------------------------+                                                
  258.  
  259. PEFLoaderInfoHeader        RECORD 0
  260. mainSection                 ds.l    1                ; offset: $0 (0)        ;  Section containing the main symbol, -1 => none. 
  261. mainOffset                 ds.l    1                ; offset: $4 (4)        ;  Offset of main symbol. 
  262. initSection                 ds.l    1                ; offset: $8 (8)        ;  Section containing the init routine's TVector, -1 => none. 
  263. initOffset                 ds.l    1                ; offset: $C (12)        ;  Offset of the init routine's TVector. 
  264. termSection                 ds.l    1                ; offset: $10 (16)        ;  Section containing the term routine's TVector, -1 => none. 
  265. termOffset                 ds.l    1                ; offset: $14 (20)        ;  Offset of the term routine's TVector. 
  266. importedLibraryCount     ds.l    1                ; offset: $18 (24)        ;  Number of imported libraries.  ('l') 
  267. totalImportedSymbolCount  ds.l    1                ; offset: $1C (28)        ;  Total number of imported symbols.  ('i') 
  268. relocSectionCount         ds.l    1                ; offset: $20 (32)        ;  Number of sections with relocations.  ('r') 
  269. relocInstrOffset         ds.l    1                ; offset: $24 (36)        ;  Offset of the relocation instructions. 
  270. loaderStringsOffset         ds.l    1                ; offset: $28 (40)        ;  Offset of the loader string table. 
  271. exportHashOffset         ds.l    1                ; offset: $2C (44)        ;  Offset of the export hash table. 
  272. exportHashTablePower     ds.l    1                ; offset: $30 (48)        ;  Export hash table size as log 2.  (Log2('h')) 
  273. exportedSymbolCount         ds.l    1                ; offset: $34 (52)        ;  Number of exported symbols.  ('e') 
  274. sizeof                     EQU *                    ; size:   $38 (56)
  275.                         ENDR
  276. ;  =========================================================================================== 
  277. ;  Imported Libraries 
  278. ;  ------------------ 
  279. PEFImportedLibrary        RECORD 0
  280. nameOffset                 ds.l    1                ; offset: $0 (0)        ;  Loader string table offset of library's name. 
  281. oldImpVersion             ds.l    1                ; offset: $4 (4)        ;  Oldest compatible implementation version. 
  282. currentVersion             ds.l    1                ; offset: $8 (8)        ;  Current version at build time. 
  283. importedSymbolCount         ds.l    1                ; offset: $C (12)        ;  Imported symbol count for this library. 
  284. firstImportedSymbol         ds.l    1                ; offset: $10 (16)        ;  Index of first imported symbol from this library. 
  285. options                     ds.b    1                ; offset: $14 (20)        ;  Option bits for this library. 
  286. reservedA                 ds.b    1                ; offset: $15 (21)        ;  Reserved, must be zero. 
  287. reservedB                 ds.w    1                ; offset: $16 (22)        ;  Reserved, must be zero. 
  288. sizeof                     EQU *                    ; size:   $18 (24)
  289.                         ENDR
  290.  
  291.                                                             ; Bits for the PEFImportedLibrary options field. 
  292. kPEFWeakImportLibMask            EQU        $40                    ; The imported library is allowed to be missing. 
  293. kPEFInitLibBeforeMask            EQU        $80                    ; The imported library must be initialized first. 
  294.  
  295. ;  =========================================================================================== 
  296. ;  Imported Symbols 
  297. ;  ---------------- 
  298.  
  299. ;  -------------------------------------------------------------------------------------------    
  300. ;  The PEFImportedSymbol type has the following bit field layout.                                
  301. ;                                                                                                  
  302. ;                                                                        3                        
  303. ;          0             7 8                                             1                        
  304. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                        
  305. ;       | symbol class  | offset of symbol name in loader string table  |                        
  306. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                        
  307. ;         |<-- 8 bits --->|<-- 24 bits ---------------------------------->|                        
  308.  
  309. PEFImportedSymbol        RECORD 0
  310. classAndName             ds.l    1                ; offset: $0 (0)
  311. sizeof                     EQU *                    ; size:   $4 (4)
  312.                         ENDR
  313.  
  314. kPEFImpSymClassShift            EQU        24
  315. kPEFImpSymNameOffsetMask        EQU        $00FFFFFF
  316. kPEFImpSymMaxNameOffset            EQU        $00FFFFFF            ; 16,777,215 
  317.  
  318.  
  319.                                                             ; Imported and exported symbol classes. 
  320. kPEFCodeSymbol                    EQU        $00
  321. kPEFDataSymbol                    EQU        $01
  322. kPEFTVectorSymbol                EQU        $02
  323. kPEFTOCSymbol                    EQU        $03
  324. kPEFGlueSymbol                    EQU        $04
  325. kPEFUndefinedSymbol                EQU        $0F
  326. kPEFWeakImportSymMask            EQU        $80
  327.  
  328. ;  =========================================================================================== 
  329. ;  Exported Symbol Hash Table 
  330. ;  -------------------------- 
  331.  
  332. ;  -------------------------------------------------------------------------------------------    
  333. ;  Exported symbols are described in four parts, optimized for speed of lookup.  These parts    
  334. ;  are the "export hash table", the "export key table", the "export symbol table", and the        
  335. ;  "export name table".  Overall they contain a flattened representation of a fairly normal        
  336. ;  hashed symbol table.                                                                            
  337. ;                                                                                                 
  338. ;  The export hash table is an array of small fixed size elements.  The number of elements is    
  339. ;  a power of 2.  A 32 bit hash word for a symbol is converted into an index into this array.    
  340. ;  Each hash slot contains a count of the number of exported symbols that map to this slot and    
  341. ;  the index of the first of those symbols in the key and symbol tables.  Of course some hash    
  342. ;  slots will have a zero count.                                                                
  343. ;                                                                                                 
  344. ;  The key and symbol tables are also arrays of fixed size elements, one for each exported        
  345. ;  symbol.  Their entries are grouped by hash slot, those elements mapping to the same hash        
  346. ;  slot are contiguous.  The key table contains just the full 32 bit hash word for each            
  347. ;  exported symbol.  The symbol table contains the offset of the symbol's name in the string    
  348. ;  table and other information about the exported symbol.                                        
  349. ;                                                                                                 
  350. ;  To look up an export you take the hashword and compute the hash slot index.  You then scan    
  351. ;  the indicated portion of the key table for matching hashwords.  If a hashword matches, you    
  352. ;  look at the corresponding symbol table entry to find the full symbol name.  If the names        
  353. ;  match the symbol is found.                                                                    
  354.  
  355. ;  -------------------------------------------------------------------------------------------    
  356. ;  The following function may be used to compute the hash table size.  Signed values are used    
  357. ;  just to avoid potential code generation overhead for unsigned division.                        
  358. ;                                                                                                  
  359. ;         UInt8    PEFComputeHashTableExponent    ( SInt32    exportCount )                            
  360. ;         {                                                                                        
  361. ;             SInt32    exponent;                                                                    
  362. ;                                                                                                  
  363. ;             const SInt32    kExponentLimit        = 16;    // Arbitrary, but must not exceed 30.    
  364. ;             const SInt32    kAverageChainLimit    = 10;    // Arbitrary, for space/time tradeoff.    
  365. ;                                                                                                  
  366. ;             for ( exponent = 0; exponent < kExponentLimit; exponent += 1 ) {                    
  367. ;                 if ( (exportCount / (1 << exponent)) < kAverageChainLimit ) break;                
  368. ;             }                                                                                    
  369. ;                                                                                                  
  370. ;             return exponent;                                                                    
  371. ;                                                                                                  
  372. ;         }    // PEFComputeHashTableExponent ()                                                    
  373.  
  374. ;  -------------------------------------------------------------------------------------------    
  375. ;  The PEFExportedSymbolHashSlot type has the following bit field layout.                        
  376. ;                                                                                                  
  377. ;                                    1 1                                 3                        
  378. ;          0                         3 4                                 1                        
  379. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                        
  380. ;         | symbol count              | index of first export key         |                        
  381. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                        
  382. ;         |<-- 14 bits -------------->|<-- 18 bits ---------------------->|                        
  383.  
  384. PEFExportedSymbolHashSlot RECORD 0
  385. countAndStart             ds.l    1                ; offset: $0 (0)
  386. sizeof                     EQU *                    ; size:   $4 (4)
  387.                         ENDR
  388.  
  389. kPEFHashSlotSymCountShift        EQU        18
  390. kPEFHashSlotFirstKeyMask        EQU        $0003FFFF
  391. kPEFHashSlotMaxSymbolCount        EQU        $00003FFF            ;  16,383 
  392. kPEFHashSlotMaxKeyIndex            EQU        $0003FFFF            ; 262,143 
  393.  
  394. ;  =========================================================================================== 
  395. ;  Exported Symbol Hash Key 
  396. ;  ------------------------ 
  397.  
  398. PEFSplitHashWord        RECORD 0
  399. nameLength                 ds.w    1                ; offset: $0 (0)
  400. hashValue                 ds.w    1                ; offset: $2 (2)
  401. sizeof                     EQU *                    ; size:   $4 (4)
  402.                         ENDR
  403. PEFExportedSymbolKey    RECORD 0
  404. fullHashWord             ds.l    1                ; offset: $0 (0)
  405.                          ORG 0
  406. splitHashWord             ds        PEFSplitHashWord ; offset: $0 (0)
  407. sizeof                     EQU *                    ; size:   $4 (4)
  408.                         ENDR
  409.  
  410. kPEFHashLengthShift                EQU        16
  411. kPEFHashValueMask                EQU        $0000FFFF
  412. kPEFHashMaxLength                EQU        $0000FFFF            ; 65,535 
  413.  
  414. ;  -------------------------------------------------------------------------------------------            
  415. ;  The following function computes the full 32 bit hash word.                                            
  416. ;                                                                                                          
  417. ;         UInt32    PEFComputeHashWord    ( BytePtr    nameText,        // ! First "letter", not length byte.    
  418. ;                                       UInt32    nameLength )    // ! The text may be zero terminated.    
  419. ;         {                                                                                                
  420. ;             BytePtr    charPtr        = nameText;                                                                
  421. ;             SInt32    hashValue    = 0;        // ! Signed to match old published algorithm.                
  422. ;             UInt32    length        = 0;                                                                    
  423. ;             UInt32    limit;                                                                                
  424. ;             UInt32    result;                                                                                
  425. ;             UInt8    currChar;                                                                            
  426. ;                                                                                                          
  427. ;             #define PseudoRotate(x)  ( ( (x) << 1 ) - ( (x) >> 16 ) )                                    
  428. ;                                                                                                          
  429. ;             for ( limit = nameLength; limit > 0; limit -= 1 ) {                                            
  430. ;                 currChar = *charPtr++;                                                                    
  431. ;                 if ( currChar == NULL ) break;                                                            
  432. ;                 length += 1;                                                                            
  433. ;                 hashValue = PseudoRotate ( hashValue ) ^ currChar;                                        
  434. ;             }                                                                                            
  435. ;                                                                                                          
  436. ;             result    = (length << kPEFHashLengthShift) |                                                    
  437. ;                       ((UInt16) ((hashValue ^ (hashValue >> 16)) & kPEFHashValueMask));                    
  438. ;                                                                                                          
  439. ;             return result;                                                                                
  440. ;                                                                                                          
  441. ;         }    // PEFComputeHashWord ()                                                                    
  442.  
  443. ;  =========================================================================================== 
  444. ;  Exported Symbols 
  445. ;  ---------------- 
  446.  
  447. PEFExportedSymbol        RECORD 0
  448. ;  ! This structure is 10 bytes long and arrays are packed. 
  449. classAndName             ds.l    1                ; offset: $0 (0)        ;  A combination of class and name offset. 
  450. symbolValue                 ds.l    1                ; offset: $4 (4)        ;  Typically the symbol's offset within a section. 
  451. sectionIndex             ds.w    1                ; offset: $8 (8)        ;  The index of the section, or pseudo-section, for the symbol. 
  452. sizeof                     EQU *                    ; size:   $A (10)
  453.                         ENDR
  454. ;  -------------------------------------------------------------------------------------------    
  455. ;  The classAndName field of the PEFExportedSymbol type has the following bit field layout.        
  456. ;                                                                                                  
  457. ;                                                                        3                        
  458. ;          0             7 8                                             1                        
  459. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                        
  460. ;       | symbol class  | offset of symbol name in loader string table  |                        
  461. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                        
  462. ;         |<-- 8 bits --->|<-- 24 bits ---------------------------------->|                        
  463.  
  464.  
  465. kPEFExpSymClassShift            EQU        24
  466. kPEFExpSymNameOffsetMask        EQU        $00FFFFFF
  467. kPEFExpSymMaxNameOffset            EQU        $00FFFFFF            ; 16,777,215 
  468.  
  469.  
  470.                                                             ; Negative section indices indicate pseudo-sections. 
  471. kPEFAbsoluteExport                EQU        -2                    ; The symbol value is an absolute address. 
  472. kPEFReexportedImport            EQU        -3                    ; The symbol value is the index of a reexported import. 
  473.  
  474. ;  =========================================================================================== 
  475. ;  Loader Relocations 
  476. ;  ================== 
  477.  
  478. ;  -------------------------------------------------------------------------------------------    
  479. ;  The relocations for a section are defined by a sequence of instructions for an abstract        
  480. ;  machine that is specifically geared to performing relocations commonly needed for the "CFM"    
  481. ;  code generation model.  These instructions occur in 16 bit chunks.  Most instructions have    
  482. ;  just a single chunk.  Instructions that are larger than 16 bits have an opcode and some of    
  483. ;  the operands in the first chunk, with other operands in following chunks.                    
  484.  
  485. ; typedef UInt16                         PEFRelocChunk
  486.  
  487. PEFLoaderRelocationHeader RECORD 0
  488. sectionIndex             ds.w    1                ; offset: $0 (0)        ;  Index of the section to be fixed up. 
  489. reservedA                 ds.w    1                ; offset: $2 (2)        ;  Reserved, must be zero. 
  490. relocCount                 ds.l    1                ; offset: $4 (4)        ;  Number of 16 bit relocation chunks. 
  491. firstRelocOffset         ds.l    1                ; offset: $8 (8)        ;  Offset of first relocation instruction. 
  492. sizeof                     EQU *                    ; size:   $C (12)
  493.                         ENDR
  494. ;  -------------------------------------------------------------------------------------------    
  495. ;  ! Note that the relocCount field is the number of 16 bit relocation chunks, i.e. 1/2 the        
  496. ;  ! total number of bytes of relocation instructions.  While most relocation instructions are    
  497. ;  ! 16 bits long, some are longer so the number of complete relocation instructions may be        
  498. ;  ! less than the relocCount value.                                                            
  499.  
  500. ;  ----------------------------------------------------------------------------------    
  501. ;  The PEFRelocField macro is a utility for extracting relocation instruction fields.    
  502.  
  503. ;  =========================================================================================== 
  504. ;  Basic Relocation Opcodes 
  505. ;  ------------------------ 
  506. ;  ------------------------------------------------------------------------------------------    
  507. ;  The number of opcode bits varies from 2 to 7.  The enumeration and switch table given here    
  508. ;  are defined in terms of the most significant 7 bits of the first instruction chunk.  An        
  509. ;  instruction is decoded by using the most significant 7 bits as an index into the opcode        
  510. ;  table, which in turn contains appropriately masked forms of the most significant 7 bits.        
  511. ;  The macro PEFRelocBasicOpcode assumes a declaration of the form.                                
  512. ;                                                                                                  
  513. ;         UInt8 kPEFRelocBasicOpcodes [kPEFRelocBasicOpcodeRange] = { PEFMaskedBasicOpcodes };    
  514.  
  515.  
  516. kPEFRelocBasicOpcodeRange        EQU        128
  517.  
  518. ;  -------------------------------------------------------------------------------------------    
  519. ;  The relocation opcodes, clustered by major and minor groups.  The instructions within a        
  520. ;  cluster all have the same bit field layout.  The enumeration values use the high order 7        
  521. ;  bits of the relocation instruction.  Unused low order bits are set to zero.                    
  522.  
  523. kPEFRelocBySectDWithSkip        EQU        $00                    ; Binary: 00x_xxxx 
  524. kPEFRelocBySectC                EQU        $20                    ; Binary: 010_0000, group is "RelocRun" 
  525. kPEFRelocBySectD                EQU        $21                    ; Binary: 010_0001 
  526. kPEFRelocTVector12                EQU        $22                    ; Binary: 010_0010 
  527. kPEFRelocTVector8                EQU        $23                    ; Binary: 010_0011 
  528. kPEFRelocVTable8                EQU        $24                    ; Binary: 010_0100 
  529. kPEFRelocImportRun                EQU        $25                    ; Binary: 010_0101 
  530. kPEFRelocSmByImport                EQU        $30                    ; Binary: 011_0000, group is "RelocSmIndex" 
  531. kPEFRelocSmSetSectC                EQU        $31                    ; Binary: 011_0001 
  532. kPEFRelocSmSetSectD                EQU        $32                    ; Binary: 011_0010 
  533. kPEFRelocSmBySection            EQU        $33                    ; Binary: 011_0011 
  534. kPEFRelocIncrPosition            EQU        $40                    ; Binary: 100_0xxx 
  535. kPEFRelocSmRepeat                EQU        $48                    ; Binary: 100_1xxx 
  536. kPEFRelocSetPosition            EQU        $50                    ; Binary: 101_000x 
  537. kPEFRelocLgByImport                EQU        $52                    ; Binary: 101_001x 
  538. kPEFRelocLgRepeat                EQU        $58                    ; Binary: 101_100x 
  539. kPEFRelocLgSetOrBySection        EQU        $5A                    ; Binary: 101_101x 
  540. kPEFRelocUndefinedOpcode        EQU        $FF                    ; Used in masking table for all undefined values. 
  541.  
  542. ;  ----------------------------------------------------------------------------    
  543. ;  The RelocLgSetOrBySection instruction has an additional 4 bits of subopcode.    
  544.  
  545. kPEFRelocLgBySectionSubopcode    EQU        $00                    ; Binary: 0000 
  546. kPEFRelocLgSetSectCSubopcode    EQU        $01                    ; Binary: 0001 
  547. kPEFRelocLgSetSectDSubopcode    EQU        $02                    ; Binary: 0010 
  548.  
  549. ;  ------------------------------------------------------------------------------------------    
  550. ;  The initial values for the opcode "masking" table.  This has the enumeration values from        
  551. ;  above with appropriate replications for "don't care" bits.  It is almost certainly shorter    
  552. ;  and faster to look up the masked value in a table than to use a branch tree.                    
  553.  
  554. ;  =========================================================================================== 
  555. ;  RelocBySectDWithSkip Instruction 
  556. ;  -------------------------------- 
  557.  
  558. ;  -------------------------------------------------------------------------------------------    
  559. ;  The "RelocBySectDWithSkip" instruction has the following bit field layout.                    
  560. ;                                                                                                  
  561. ;                              1         1                                                        
  562. ;          0 1 2             9 0         5                                                        
  563. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                                                        
  564. ;         |0 0| skip count    | rel count |                                                        
  565. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                                                        
  566. ;         | 2 |<-- 8 bits --->|<--  6 --->|                                                        
  567. ;                                                                                                  
  568. ;  ! Note that the stored skip count and reloc count are the actual values!                        
  569.  
  570. kPEFRelocWithSkipMaxSkipCount    EQU        255
  571. kPEFRelocWithSkipMaxRelocCount    EQU        63
  572.  
  573. ;  =========================================================================================== 
  574. ;  RelocRun Group 
  575. ;  -------------- 
  576.  
  577. ;  -------------------------------------------------------------------------------------------    
  578. ;  The "RelocRun" group includes the "RelocBySectC", "RelocBySectD", "RelocTVector12",            
  579. ;  "RelocTVector8", "RelocVTable8", and "RelocImportRun" instructions.  This group has the        
  580. ;  following bit field layout.                                                                    
  581. ;                                                                                                  
  582. ;                                        1                                                        
  583. ;          0   2 3     6 7               5                                                        
  584. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                                                        
  585. ;         |0 1 0| subop.| run length      |                                                        
  586. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                                                        
  587. ;         |  3  |<- 4 ->|<-- 9 bits ----->|                                                        
  588. ;                                                                                                  
  589. ;  ! Note that the stored run length is the actual value minus 1, but the macros deal with the    
  590. ;  ! actual value!                                                                                
  591.  
  592. kPEFRelocRunMaxRunLength        EQU        512
  593.  
  594. ;  =========================================================================================== 
  595. ;  RelocSmIndex Group 
  596. ;  ------------------ 
  597.  
  598. ;  ----------------------------------------------------------------------------------------    
  599. ;  The "RelocSmIndex" group includes the "RelocSmByImport", "RelocSmSetSectC",                
  600. ;  "RelocSmSetSectD" and "RelocSmBySection" instructions.  This group has the following bit    
  601. ;  field layout.                                                                            
  602. ;                                                                                              
  603. ;                                        1                                                    
  604. ;          0   2 3     6 7               5                                                    
  605. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                                                    
  606. ;         |0 1 1| subop.| index           |                                                    
  607. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                                                    
  608. ;         |  3  |<- 4 ->|<-- 9 bits ----->|                                                    
  609. ;                                                                                              
  610. ;  ! Note that the stored index is the actual value!                                        
  611.  
  612. kPEFRelocSmIndexMaxIndex        EQU        511
  613.  
  614. ;  =========================================================================================== 
  615. ;  RelocIncrPosition Instruction 
  616. ;  ----------------------------- 
  617.  
  618. ;  -------------------------------------------------------------------------------------------    
  619. ;  The "RelocIncrPosition" instruction has the following bit field layout.                        
  620. ;                                                                                                  
  621. ;                                        1                                                        
  622. ;          0     3 4                     5                                                        
  623. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                                                        
  624. ;         |1 0 0 0| offset                |                                                        
  625. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                                                        
  626. ;         |<- 4 ->|<-- 12 bits ---------->|                                                        
  627. ;                                                                                                  
  628. ;  ! Note that the stored offset is the actual value minus 1, but the macros deal with the        
  629. ;  ! actual value!                                                                                
  630.  
  631. kPEFRelocIncrPositionMaxOffset    EQU        4096
  632.  
  633. ;  =========================================================================================== 
  634. ;  RelocSmRepeat Instruction 
  635. ;  ------------------------- 
  636.  
  637. ;  -------------------------------------------------------------------------------------------    
  638. ;  The "RelocSmRepeat" instruction has the following bit field layout.                            
  639. ;                                                                                                  
  640. ;                                        1                                                        
  641. ;          0     3 4     7 8             5                                                        
  642. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                                                        
  643. ;         |1 0 0 1| chnks | repeat count  |                                                        
  644. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                                                        
  645. ;         |<- 4 ->|<- 4 ->|<-- 8 bits --->|                                                        
  646. ;                                                                                                  
  647. ;  ! Note that the stored chunk count and repeat count are the actual values minus 1, but the    
  648. ;  ! macros deal with the actual values!                                                        
  649.  
  650. kPEFRelocSmRepeatMaxChunkCount    EQU        16
  651. kPEFRelocSmRepeatMaxRepeatCount    EQU        256
  652.  
  653. ;  =========================================================================================== 
  654. ;  RelocSetPosition Instruction 
  655. ;  ---------------------------- 
  656.  
  657. ;  -------------------------------------------------------------------------------------------    
  658. ;  The "RelocSetPosition" instruction has the following bit field layout.                        
  659. ;                                                                                                  
  660. ;                                        1                                   1                    
  661. ;          0         5 6                 5     0                             5                    
  662. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                    
  663. ;         |1 0 1 0 0 0| offset (high)     |   | offset (low)                  |                    
  664. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                    
  665. ;         |<-- 6 ---->|<-- 10 bits ------>|   |<-- 16 bits ------------------>|                    
  666. ;                                                                                                  
  667. ;  ! Note that the stored offset is the actual value!                                            
  668.  
  669. kPEFRelocSetPosMaxOffset        EQU        $03FFFFFF            ; 67,108,863 
  670.  
  671. ;  =========================================================================================== 
  672. ;  RelocLgByImport Instruction 
  673. ;  --------------------------- 
  674.  
  675. ;  -------------------------------------------------------------------------------------------    
  676. ;  The "RelocLgByImport" instruction has the following bit field layout.                        
  677. ;                                                                                                  
  678. ;                                        1                                   1                    
  679. ;          0         5 6                 5     0                             5                    
  680. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                    
  681. ;         |1 0 1 0 0 1| index (high)      |   | index (low)                   |                    
  682. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                    
  683. ;         |<-- 6 ---->|<-- 10 bits ------>|   |<-- 16 bits ------------------>|                    
  684. ;                                                                                                  
  685. ;  ! Note that the stored offset is the actual value!                                            
  686.  
  687. kPEFRelocLgByImportMaxIndex        EQU        $03FFFFFF            ; 67,108,863 
  688.  
  689. ;  =========================================================================================== 
  690. ;  RelocLgRepeat Instruction 
  691. ;  ------------------------- 
  692.  
  693. ;  -------------------------------------------------------------------------------------------    
  694. ;  The "RelocLgRepeat" instruction has the following bit field layout.                            
  695. ;                                                                                                  
  696. ;                              1         1                                   1                    
  697. ;          0         5 6     9 0         5     0                             5                    
  698. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                    
  699. ;         |1 0 1 1 0 0| chnks | rpt (high)|   | repeat count (low)            |                    
  700. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                    
  701. ;         |<--  6 --->|<- 4 ->|<--  6 --->|   |<-- 16 bits ------------------>|                    
  702. ;                                                                                                  
  703. ;  ! Note that the stored chunk count is the actual value minus 1, but the macros deal with        
  704. ;  ! the actual value!  The stored repeat count is the actual value!                            
  705.  
  706. kPEFRelocLgRepeatMaxChunkCount    EQU        16
  707. kPEFRelocLgRepeatMaxRepeatCount    EQU        $003FFFFF            ; 4,194,303 
  708.  
  709. ;  =========================================================================================== 
  710. ;  RelocLgSetOrBySection Group 
  711. ;  --------------------------- 
  712.  
  713. ;  -------------------------------------------------------------------------------------------    
  714. ;  The "RelocLgSetOrBySection" instruction is really a group including the "RelocLgBySection",    
  715. ;  "RelocLgSetSectC" and "RelocLgSetSectD" instructions.  This group has the following bit        
  716. ;  field layout.                                                                                
  717. ;                                                                                                  
  718. ;                              1         1                                   1                    
  719. ;          0         5 6     9 0         5     0                             5                    
  720. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                    
  721. ;         |1 0 1 1 0 1| subop | idx (high)|   | index (low)                   |                    
  722. ;         +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                    
  723. ;         |<--  6 --->|<- 4 ->|<--  6 --->|   |<-- 16 bits ------------------>|                    
  724. ;                                                                                                  
  725. ;  ! Note that the stored index is the actual value!                                            
  726.  
  727. kPEFRelocLgSetOrBySectionMaxIndex EQU    $003FFFFF            ; 4,194,303 
  728.  
  729.  
  730.     ENDIF ; __PEFBINARYFORMAT__ 
  731.  
  732.